package club.hicode.daydayup.zookeeper;

import cn.hutool.core.thread.ThreadUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * 通过缓存监听节点变化
 *
 * @author Liu Chunfu
 * @date 2018-06-01 下午6:03
 **/
public class CacheTest {

    //private static final String CONNECT_STR = "127.0.0.1:2181";
    private static final String CONNECT_STR = "39.108.231.92:2181";


    @Test
    public void nodeCacheTest() throws Exception {
        String path = "/hicode/club";
        CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new RetryNTimes(5, 2000))
                .connectString(CONNECT_STR).build();
        client.start();
        //通过 client 创建 nodeCache，请注意一定要 start
        NodeCache nodeCache = new NodeCache(client, path);
        nodeCache.start();
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("nodeChanged...");
            }
        });

        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + "/" + "test");
        client.setData().forPath(path + "/test", "cc".getBytes());
        client.setData().forPath(path + "/test", "hh".getBytes());
        client.delete().forPath(path + "/test");
        ThreadUtil.sleep(5, TimeUnit.MINUTES);

        nodeCache.close();
    }


    @Test
    public void pathCacheTest() throws Exception {
        String path = "/hicode/club/childPath";
        CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new RetryNTimes(5, 2000))
                .connectString(CONNECT_STR).build();
        client.start();
        PathChildrenCache pathCache = new PathChildrenCache(client, path, false);
        pathCache.start();

        //默认会有事件：PathChildrenCacheEvent{type=CONNECTION_RECONNECTED, data=null}
        pathCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                System.out.println("事件类型是：" + event);
            }
        });


        //创建节点，不触发
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path);
        //对节点赋值，不触发
        client.setData().forPath(path, "childPath".getBytes());

        String tempPath = path + "/temp";
        //创建节点触发
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(tempPath);
        client.setData().forPath(tempPath, "666".getBytes());

        ThreadUtil.sleep(5, TimeUnit.MINUTES);
        pathCache.close();
        client.close();
    }

    @Test
    public void testEmp() throws Exception {
        String path = "/hicode/club/childPath";
        CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new RetryNTimes(5, 2000))
                .connectString(CONNECT_STR).build();
        client.start();

        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + "/hh/xxx", "xxx".getBytes());
        ThreadUtil.sleep(5, TimeUnit.MINUTES);
    }


    @Test
    public void treeCacheTest() throws Exception {
        String path = "/hicode/club/tree";
        CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new RetryNTimes(5, 2000))
                .connectString(CONNECT_STR).build();
        client.start();
        TreeCache treeCache = new TreeCache(client, path);
        treeCache.start();
        //默认会有事件：PathChildrenCacheEvent{type=CONNECTION_RECONNECTED, data=null}
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                System.out.println("事件类型是：" + event);
            }
        });
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + "/t1", "xxx".getBytes());
        //client.setData().forPath(path, "childPath".getBytes());
        ThreadUtil.sleep(1, TimeUnit.SECONDS);
        System.out.println("==============");
        String tempPath = path + "/t2";
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(tempPath, "xxx".getBytes());
        client.setData().forPath(tempPath, "666".getBytes());
        client.delete().forPath(tempPath);
        ThreadUtil.sleep(5, TimeUnit.MINUTES);
        treeCache.close();
        client.close();
    }

}
